package com.lyx.crm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lyx.crm.annotation.RequiredPermission;
import com.lyx.crm.base.BaseController;
import com.lyx.crm.base.ResultInfo;
import com.lyx.crm.exceptions.ParamsException;
import com.lyx.crm.model.UserModel;
import com.lyx.crm.query.UserQuery;
import com.lyx.crm.service.UserService;
import com.lyx.crm.utils.LoginUserUtil;
import com.lyx.crm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @Package: com.lyx.crm.controller
 * @ClassName: UserController
 * @Author: lyx
 * @CreateTime: 2021/7/15 20:14
 * @Description:
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录功能
     *
     * @param userName
     * @param userPwd
     * @return
     */
    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName, String userPwd) {
        ResultInfo resultInfo = new ResultInfo();

        try {
            UserModel userModel = userService.userLogin(userName, userPwd);
            resultInfo.setResult(userModel);

        } catch (ParamsException p) {
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg("登录失败");
            e.printStackTrace();
        }
        return resultInfo;
    }


    @PostMapping("updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword, String newPassword, String repeatPassword) {
        ResultInfo resultInfo = new ResultInfo();

        try {
            //获取cookie中用户id,
            int userId = LoginUserUtil.releaseUserIdFromCookie(request);
            userService.updatePassWord(userId, oldPassword, newPassword, repeatPassword);
        } catch (ParamsException p) {
            resultInfo.setCode(500);
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg("修改密码失败");
            e.printStackTrace();
        }
        return resultInfo;

    }

    @RequestMapping("toPasswordPage")
    public String toPassword() {

        return "user/password";
    }

    @RequestMapping("query")
    @ResponseBody
    public List<Map<String, Object>> queryAllSales() {
        return userService.queryAllSales();
    }

    /**
     * 多条件查询用户
     *
     * @param userQuery
     * @return
     */
    @RequiredPermission(code = "601002")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> selectByParams(UserQuery userQuery) {
        return userService.queryByParamsForTable(userQuery);

    }

    /**
     * 进入列表页面
     *
     * @return
     */
    @RequiredPermission(code = "6010")
    @RequestMapping("index")
    public String index() {

        return "user/user";
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @RequiredPermission(code = "601001")
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addUser(User user) {
        userService.addUser(user);
        return success("用户添加成功");
    }

    /**
     * 打开添加/修改用户的页面
     *
     * @return
     */
    @RequestMapping("toAddOrUpdateUserPage")
    public String toAddOrUpdateUserPage(Integer id,HttpServletRequest request) {
        if(null != id){
            User user = userService.selectByPrimaryKey(id);
            if(!ObjectUtil.isEmpty(user)){
                request.setAttribute("userInfo",user);
            }
        }

        return "user/add_update";
    }

    /**
     * 修改用户操作
     * @param user
     * @return
     */
    @RequiredPermission(code = "601003")
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user) {
        userService.updateUser(user);
        return success("用户更新成功");

    }

    /**
     * 删除用户操作
     * @param ids
     * @return
     */
    @RequiredPermission(code = "601004")
    @ResponseBody
    @RequestMapping("delete")
    public ResultInfo deleteUser(Integer[] ids){
        userService.deleteByIds(ids);
        return success("用户删除成功");
    }
}
